#include "MyVector.h"

MyVector::MyVector() : maxsize(10), size(0), pdata(new std::string[maxsize]) {}

MyVector::MyVector(const MyVector& other) : maxsize(other.maxsize), size(other.size), pdata(new std::string[other.maxsize]) {
    for (int i = 0; i < size; ++i) {
        pdata[i] = other.pdata[i];
    }
}

MyVector::~MyVector() {
    delete[] pdata;
}

void MyVector::add_element(const std::string& element) {
    if (size == maxsize) {
        resize();
    }
    pdata[size++] = element;
}

void MyVector::delete_element(int index) {
    if (index >= 0 && index < size) {
        for (int i = index; i < size - 1; ++i) {
            pdata[i] = pdata[i + 1];
        }
        size--;
    }
}

int MyVector::find(const std::string& element) const {
    for (int i = 0; i < size; ++i) {
        if (pdata[i] == element) {
            return i;
        }
    }
    return -1;
}

std::string& MyVector::operator[](int index) {
    if (index >= 0 && index < size) {
        return pdata[index];
    } else {
        return pdata[0];
    }
}

MyVector& MyVector::operator=(const MyVector& other) {
    if (this == &other) {
        return *this;
    }

    delete[] pdata;
    maxsize = other.maxsize;
    size = other.size;
    pdata = new std::string[maxsize];
    
    for (int i = 0; i < size; ++i) {
        pdata[i] = other.pdata[i];
    }

    return *this;
}

void MyVector::resize() {
    int newMaxSize = maxsize * 2;
    std::string* newData = new std::string[newMaxSize];

    for (int i = 0; i < size; ++i) {
        newData[i] = pdata[i];
    }

    delete[] pdata;
    pdata = newData;
    maxsize = newMaxSize;
}
